import {defineStore} from 'pinia'
import {ref} from 'vue'
import {
    addConfig, deleteConfig,
    getConfig,
    getConfigList, updateConfig,

} from "@/api/config.js";

export const useConfigStore = defineStore('config', () => {

    const loading = ref(false)

    async function fetchConfigList(params) {
        loading.value = true
        try {
            return await getConfigList(params)
        } catch (error) {
            // console.log('获取文章列表失败')
        } finally {
            loading.value = false
        }
    }

    async function fetchConfig(key) {
        loading.value = true
        try {
            return await getConfig(key)
        } catch (error) {
            // console.log('获取文章列表失败')
        } finally {
            loading.value = false
        }
    }

    async function fetchAddConfig(config) {
        loading.value = true
        try {
            return await addConfig(config)
        } catch (error) {
            // console.log('获取文章列表失败')
        } finally {
            loading.value = false
        }
    }

    async function fetchUpdateConfig(config) {
        loading.value = true
        try {
            return await updateConfig(config)
        } catch (error) {
            // console.log('获取文章列表失败')
        } finally {
            loading.value = false
        }
    }

    async function fetchDeleteConfig(key) {
        loading.value = true
        try {
            return await deleteConfig(key)
        } catch (error) {
            // console.log('获取文章列表失败')
        } finally {
            loading.value = false
        }
    }

    return {
        fetchConfigList,
        fetchConfig,
        fetchAddConfig,
        fetchUpdateConfig,
        fetchDeleteConfig
    }

})